package com.jtpay.sdk.weight;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.jtpay.sdk.R;
import com.jtpay.sdk.adapter.UserNamePopAdapter;
import com.jtpay.sdk.entity.UserModel;

import java.util.List;


/**
 * Created by Administrator on 2017/8/29.
 * Username Popupwindow,Used to display the login success username
 */

public class UserNamePop implements View.OnClickListener {
    private UserNamePopAdapter mAdapter;
    private List<UserModel> mUserModels;
    private Context mContext;
    private PopupWindow mPopupWindow;
    private ListView mListView;
    private Button mClearButton;
    private BottomClickListener mListener;

    public UserNamePop(List<UserModel> UserModels, Context context, UserNamePopAdapter.ItemClickListener listener) {
        mUserModels = UserModels;
        mContext = context;
        View view = LayoutInflater.from(context).inflate(R.layout.pop_username, null);
        mListView = (ListView) view.findViewById(R.id.listView_username);
        mClearButton = (Button) view.findViewById(R.id.clear_botton);
        mClearButton.setOnClickListener(this);
        mAdapter = new UserNamePopAdapter(mContext, mUserModels);
        mAdapter.setListener(listener);
        mListView.setAdapter(mAdapter);
        mListView.setFocusableInTouchMode(true);
        mListView.setFocusable(true);
        mPopupWindow = new PopupWindow(view, context.getResources().getDimensionPixelSize(R.dimen.popmenu_width), context.getResources().getDimensionPixelSize(R.dimen.popmenu_width));
        //Clicking on "Back" can also make it disappear
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        // Focusing
        mPopupWindow.setFocusable(true);
        // Settings allow click to disappear
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
        mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    }

    public void setListener(BottomClickListener listener) {
        mListener = listener;
    }

    // showAtLocation
    public void showAtLocation(View parent) {
        int[] positon = new int[2];
        parent.getLocationInWindow(positon);
        mPopupWindow.showAtLocation(parent, Gravity.NO_GRAVITY, positon[0] - mPopupWindow.getWidth(), positon[1] + parent.getHeight());
    }

    // hidden menu
    public void dismiss() {
        if (mPopupWindow.isShowing())
            mPopupWindow.dismiss();
    }

    public void notifyDataSetChanged() {
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onClick(View v) {
        if (mListener != null) {
            mListener.cleanClick();
        }
    }

    public interface BottomClickListener {
        void cleanClick();
    }
}
